import requests

from apps.pdd.pdd.utils.js import gen_anti_content


class MmsRetrievePassword:
    session = requests.session()

    def __init__(self, cookies=None):
        headers = {
            "accept": "*/*",
            "accept-language": "zh-CN,zh;q=0.9",
            # "anti-content": "0asAfxnqNjIyJ9E2Y4Mpx0KN4pybBMgZJwXUqwnyJe5Hn-VakLxbz0jfs8Duz-pYQP8jM1THVyLwgdN59MDB6VaP1C7QECSMVpsppW-IstGL69D1tZ6Aw--b2NCqoXWAQCR0qqS9N0RCLYSvvaxbvMraDe3L1b2jDeeLSU2QYq712d0Qh1hdCy0MhShduNXclBhdWHBjQapjBd4lNoRqVnbxAqVDTGJ-sYbUX9ZNajWgG54rBtuo0_q5uHgYO2DcgWCfYyeeyqEKeHbeNf7cSBJpnZZQR442eNNrF5TSnY5Eoh1u2JWzQ4lJHTu4qENkID6zracorI69ahzQfLJ8X4-0kYKuWaOoo1iVOXlO63kuc2oxiUfE2AQtG6oclckpVWQLrI0-k1wMx2Amq-ZNJKzyJttb_S8DxclbC3l3MUhSf_VNNc5brJvon_2e6OEFBn_n4wsSZsw5gMxHv9yPCMVZ4wfE5o8zlJnusd0azasIvVEf-Fx7OzyG2YV20EbbVy5Uo3rAKCn4eqTq7S0zAK-xrGdoWkeK1kXttXKjAd5b_C7FOFCt5-m655tk-KfEdN1cwGKE2Q5nOgvQV_lmdtI80QeHkxOYFcK25l-75BgcfdwislkSsOBYfafg1dGTNvyyDs_FWbVEKWydpGqJN-FToMjwPg7jWwezAomtxT_KKTmL4hyi4o6rhh-N5m73s7Ei0dwYot4aNylxf0fZoY",
            # "cache-control": "no-cache",
            # "etag": "0KnmGQbIBxM965CND9n283P3xPfBzfER",
            "origin": "https://mms.pinduoduo.com",
            "pragma": "no-cache",
            "priority": "u=1, i",
            "referer": "https://mms.pinduoduo.com/mallcenter/resetPwd/index",
            "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Windows"',
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
        }
        self.session.headers = headers

    def nologin(self, mobile):
        """
        发送短息
        {"success":true,"errorCode":1000000,"errorMsg":null,"result":null}
        """
        url = "https://mms.pinduoduo.com/janus/api/mobile/sendVerificationCode/noLogin"
        data = {
            "type": 15,
            "mobile": mobile,
            "crawlerInfo": "0asAfxnqNjIyJ9E2Y4Mpx0KN4pybBMgZJwXUqwnyJe5Hn-VakLxbz0jfs8Duz-pYQP8jM1THVyLwgdN59MDB6VaP1C7QECSMVpsppW-IstGL69D1tZ6Aw--b2NCqoXWAQCR0qqS9N0RCLYSvvaxbvMraDe3L1b2jDeeLSU2QYq712d0Qh1hdCy0MhShduNXclBhdWHBjQapjBd4lNoRqVnbxAqVDTGJ-sYbUX9ZNajWgG54rBtuo0_q5uHgYO2DcgWCfYyeeyqEKeHbeNf7cSBJpnZZQR442eNNrF5TSnY5Eoh1u2JWzQ4lJHTu4qENkID6zracorI69ahzQfLJ8X4-0kYKuWaOoo1iVOXlO63kuc2oxiUfE2AQtG6oclckpVWQLrI0-k1wMx2Amq-ZNJKzyJttb_S8DxclbC3l3MUhSf_VNNc5brJvon_2e6OEFBn_n4wsSZsw5gMxHv9yPCMVZ4wfE5o8zlJnusd0azasIvVEf-Fx7OzyG2YV20EbbVy5Uo3rAKCn4eqTq7S0zAK-xrGdoWkeK1kXttXKjAd5b_C7FOFCt5-m655tk-KfEdN1cwGKE2Q5nOgvQV_lmdtI80QeHkxOYFcK25l-75BgcfdwislkSsOBYfafg1dGTNvyyDs_FWbVEKWydpGqJN-FToMjwPg7jWwezAomtxT_KKTmL4hyi4o6rhh-N5m73s7Ei0dwYot4aNylxf0fZoY",
        }
        self.session.headers.update({"Anti-Content": gen_anti_content()})  # 0as
        response = self.session.post(url, json=data)
        print(response.json())

    def retrieve_password_check(self, mobile, sms_code):
        """
        {"success":false,"errorCode":4000002,"errorMsg":"没有找到对应的账号，请确认您输入的信息是否有误","result":null}
        {"success":true,"errorCode":1000000,"errorMsg":null,"result":{"verifyToken":"5bcc7eab93954d47b21491af98237bc0","needCheckIdCard":true}}
        {"success":true,"errorCode":1000000,"errorMsg":null,"result":{"verifyToken":"45593bfde131460085a773012ab9af35","needCheckIdCard":false}}
        {"success":false,"errorCode":8000043,"errorMsg":"该手机号已绑定多个账号，暂不支持找回密码","result":null}
        """
        url = "https://mms.pinduoduo.com/janus/api/user/retrievePasswordCheckV2"
        data = {"mobile": mobile, "verificationCode": sms_code, "mallOwner": True}
        self.session.headers.update({"Anti-Content": gen_anti_content()})  # 0as
        response = self.session.post(url, json=data)
        print(response.json())


class MmsLogin:
    pass


class MmsRegister:
    def send_sms_code(self):
        url = "https://mms.pinduoduo.com/earth/api/sendSms/sendMmsCreateMallVerificationCode"
        data = {"mobile": "17784634346"}
        headers = {
            "accept": "*/*",
            "accept-language": "zh-CN,zh;q=0.9",
            "anti-content": "0apAfx5e-wCEGa1OfrFluXxQEeWIlRErUda7BuuxoaiMU6x2K2hp8TTl7aIY_PLSL_USnVt7YNJcjZoP0XoKpTWZQ__NJQjmOGpXOQyZjGpXbcGdSoHZjfpXSc_Uyc4ZJfHaeKd5wjv5uq94gXId4a5QYAvTW2lNpL62o3e623qxUD-eKEBVhk-2KkzFHE-xIK9KS-2HD-F-EBV-vs9CyQg70DG3E-v-ezs5273ooFv-pU3ZW1-ACEtBhs9SOQGZx7NTVTX0WTXI2z99-E-jCSveZkk4Sbs2p-KbkFfJKUFR1DB3MIBeCSkJVbVo5bL45-e8kUtr1Mf8W-k4Hbewd96LwHkzRCeCt24mBfkEFg-vgh0wUw2D-zACzvV7-BPwe3Kw63A_GqQEvwPmvZ8Heh5W841HDAru8LUD8C6y_SM-l9l8uYgMfYUj0xC19t0cYZjft9NwDNbnpUOG8z2V6l-5ysgAteo1MLbEzLCmj2wmD-eEr1crdIlDEaYdH3YuTX9R4luCEnILWbO",
            "cache-control": "no-cache",
            "content-type": "application/json",
            "origin": "https://mms.pinduoduo.com",
            "pragma": "no-cache",
            "priority": "u=1, i",
            "referer": "https://mms.pinduoduo.com/login/register?redirectUrl=https%3A%2F%2Fmms.pinduoduo.com%2Fhome",
            "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Windows"',
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
        }
        cookies = {
            "api_uid": "CiL0XWiMe/W1mQCRrsw6Ag==",
            "rckk": "0KnmGQbIBxM965CND9n283P3xPfBzfER",
            "_bee": "0KnmGQbIBxM965CND9n283P3xPfBzfER",
            "ru1k": "f4f488d4-f4cc-4579-99c2-c8af6fe16c21",
            "_f77": "f4f488d4-f4cc-4579-99c2-c8af6fe16c21",
            "ru2k": "15ca269b-5d9d-44dd-852d-646c2225b362",
            "_a42": "15ca269b-5d9d-44dd-852d-646c2225b362",
            "_nano_fp": "Xpmyn09qnqT8n5XonT_L1ZTsVk3FAOQe3KGKmYMU",
        }
        response = requests.post(url, headers=headers, cookies=cookies, json=data)

    def register(self):
        """
        {
            "success": false,
            "errorCode": 2001004,
            "errorMsg": "该手机号已绑定了店铺的子账号，店铺名：棉*丫，子账户名：棉娃丫***11；请前往商家后台使用子账户名登录",
            "result": null
        }
        """
        headers = {
            "accept": "*/*",
            "accept-language": "zh-CN,zh;q=0.9",
            "anti-content": "0apWtxUkM_VeXaKAt4-29Q68eanUjMWnhNbaN5VRtU-0CQuObanfKAo1z-sk3pvB4_mLWoS3ZmHB7dM1kdM23BWuSLhCF3RWMDWC-BIWMkxp7I_CzBIpMk_DSttDB6QXz5dIH6UI1sms6BK4e1-3QWMnVUWGArSyn295NlgYcUfqng2jngpanXnJlUsjsYEylYvxngeJlXuxngwfGg4f7fhMREqxnHnqFG65SYC9GHnStxsCzni2a7luGg_RWEB0SbZ0Ke6WVm70F24nxlHZwKYsyyBxbG0Sldaj0doY4tiv8lYT7nY4wjoUx4CPx8BSnYOz2EJjQdOCsyB3ba5KfTygeyci2x2v0sbnp8xG4QI1vsUK50JnO92OU0onnw5YYw5TqNFJOuaGIwbXloeYAs9nlEe8iyrlifJcflK-_lnufAOmMtQdStIPU4vVuBmSBKpvtL5E3qDStREOFg0QATPf0fNyYAzTiaali23IG5bJnaxJAk8tAu8xZF13rML2vCzMJIgDRSR9",
            "cache-control": "no-cache",
            "content-type": "application/json",
            "origin": "https://mms.pinduoduo.com",
            "pragma": "no-cache",
            "priority": "u=1, i",
            "referer": "https://mms.pinduoduo.com/login/register?redirectUrl=https%3A%2F%2Fmms.pinduoduo.com%2Fhome",
            "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Windows"',
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
        }
        cookies = {
            "api_uid": "CiL0XWiMe/W1mQCRrsw6Ag==",
            "rckk": "0KnmGQbIBxM965CND9n283P3xPfBzfER",
            "_bee": "0KnmGQbIBxM965CND9n283P3xPfBzfER",
            "ru1k": "f4f488d4-f4cc-4579-99c2-c8af6fe16c21",
            "_f77": "f4f488d4-f4cc-4579-99c2-c8af6fe16c21",
            "ru2k": "15ca269b-5d9d-44dd-852d-646c2225b362",
            "_a42": "15ca269b-5d9d-44dd-852d-646c2225b362",
            "_nano_fp": "Xpmyn09qnqT8n5XonT_L1ZTsVk3FAOQe3KGKmYMU",
        }
        url = "https://mms.pinduoduo.com/janus/api/register"
        data = {
            "mobile": "17784634346",
            "code": "228940",
            "password": "uQeMC6urIgSVk9ScxodJLNMZkKULy9Y0SP4nKS8AQ4Qw+M+y+DtEj0l5+zIH9yobQU4Mdddb+t2PcXKaumS5CmiyreAEPRJvdgFEygQ04iwtoROCwee1xLm1ThyncTM0n+nuz/qsMWF2Ulwyrto+MSG2ker+lYrZJvpBFGW5bDU=",
            "overseaType": 0,
            "riskSign": "iUfpiMh8+6XSiBAkFVcQV4slWK5Gx7Ww/eKwarzjANfjPVojMVPVL6Cfx78BeOfcLDoXtgvWsLIxhpuZahCt0PwWQljWbenP7ME7rHj017sfdluJLLOwhObo0oe0xTRgmSQIl3SNUG9hheNBxZm0K8KVUexd/ame6wi3o7KAEX4=",
            "timestamp": 1754037973985,
        }
        response = requests.post(url, headers=headers, cookies=cookies, json=data)
